import userDB from "../db/UserDB";
import bcryptjs from "bcryptjs"

class User {
    private _id: string;
    private _username: string;
    private _password: string;

    constructor(id: string, username: string, password: string) {
        this._id = id;
        this._username = username;
        this._password = password;
    }

    public getId(): string {
        return this._id;
    }

    public getUsername(): string {
        return this._username;
    }

    public static async getFriends(userId: string):Promise<string[]>{
        const user = await userDB.findOne({userId: userId});
        if(user === null) return [];
        return user.friends;
    }

    public static async getName(userId: string):Promise<string>{
        const user = await userDB.findOne({userId: userId});
        if(user === null) return "";
        return user.userName;
    }

    public static async CheckPassword(userId:string,password:string):Promise<boolean>{
        try {
            const user = await userDB.findOne({ userId }, 'password');
            if(user === null)
            {
                return false;
            }else
            {
                return bcryptjs.compareSync(password,user.password);
            }
        } catch (error) {
            console.error('Error fetching password:', error);
            return false;
        }
    }

    

    public static async CheckUserExist(userName: string): Promise<boolean> {
        return await userDB.findOne({userName: userName}).then(user => !!user).catch(() => false); // !!:将object转换为boolean
    }

    public static async CheckUserIdExist(userId: string): Promise<boolean> {
        return await userDB.findOne({userId: userId}).then(user => !!user).catch(() => false); // !!:将object转换为boolean
    }

    public async insertNewUser() {
        await userDB.create({
            userId: this._id,
            userName: this._username,
            password: this._password
        })
    }
    // 添加好友的方法
    public static async addFriend(userId: string, friendId: string): Promise<boolean> {
        try {
            // 确保双方都存在
            const user = await userDB.findOne({ userId:userId });
            const friend = await userDB.findOne({ userId: friendId });
            console.log(userId);
            console.log(friendId);
            if (user === null || friend=== null) {
                return false;  // 用户或好友不存在
            }
            // 检查是否已经是好友
            if (user.friends.includes(friendId) || friend.friends.includes(userId)) {
                return false;  // 如果已经是好友，返回 false
            }
            // 添加好友关系
            await userDB.updateOne(
                { userId },
                { $push: { friends: friendId } }  // 向当前用户的 friends 数组中推送好友ID
            );

            await userDB.updateOne(
                { userId: friendId },
                { $push: { friends: userId } }  // 向好友的 friends 数组中推送当前用户ID
            );
            return true;  // 成功添加好友
        } catch (error) {
            console.error('Error adding friend:', error);
            return false;
        }
    }


}

export default User;